package com.cloudeasy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudeasy.user.util.IPageUtil;
import com.cloudeasy.user.constant.AvatarConstant;
import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.exception.CloudEasyException;
import com.cloudeasy.user.dto.UserInfoDTO;
import com.cloudeasy.user.mapper.UserInfoMapper;
import com.cloudeasy.user.po.UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserInfoService extends ServiceImpl<UserInfoMapper, UserInfo> {
    /**
     * 判断昵称是否存在
     *
     * @param nickname 昵称
     * @return {@link Boolean}
     */
    public Boolean existsNickname(String nickname) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nickname", nickname);
        return this.getOne(queryWrapper) != null;
    }

    /**
     * 保存用户信息
     *
     * @param id       用户Id
     * @param nickname 昵称
     * @return {@link Boolean}
     */
    public Boolean save(String id, String nickname) {
        if (existsNickname(nickname)) {
            throw new CloudEasyException(ResultCodeEnum.NAME_EXISTS);
        }
        UserInfo userInfo = UserInfo.builder()
                .id(id)
                .nickname(nickname)
                .avatar(AvatarConstant.DEFAULT_AVATAR)
                .build();
        return this.save(userInfo);
    }

    /**
     * 更新用户信息
     *
     * @param userId      用户Id
     * @param userInfoDTO {@link UserInfoDTO}
     * @return {@link Boolean}
     */
    public Boolean update(String userId, UserInfoDTO userInfoDTO) {
        UserInfo userInfo = getById(userId);
        if (!userInfo.getNickname().equals(userInfoDTO.getNickname())&&existsNickname(userInfoDTO.getNickname())) {
            throw new CloudEasyException(ResultCodeEnum.NAME_EXISTS);
        }
        BeanUtils.copyProperties(userInfoDTO, userInfo);
        return this.updateById(userInfo);
    }

    /**
     * 关注
     *
     * @param userId      用户Id
     * @param attentionId 关注的用户Id
     */
    public void attention(String userId, String attentionId) {
        UserInfo userInfo = this.getById(userId);
        userInfo.setUserAttentionCount(userInfo.getUserAttentionCount() + 1);
        this.updateById(userInfo);
        userInfo = this.getById(attentionId);
        userInfo.setFansCount(userInfo.getFansCount() + 1);
        this.updateById(userInfo);
    }

    /**
     * 移除关注
     *
     * @param userId      用户Id
     * @param attentionId 关注的用户Id
     */
    public void removeAttention(String userId, String attentionId) {
        UserInfo userInfo = this.getById(userId);
        userInfo.setUserAttentionCount(userInfo.getUserAttentionCount() - 1);
        this.updateById(userInfo);
        userInfo = this.getById(attentionId);
        userInfo.setFansCount(userInfo.getFansCount() - 1);
        this.updateById(userInfo);
    }

    /**
     * 登录
     * 更新最后登录时间
     *
     * @param userId 登录用户Id
     */
    public void login(String userId) {
        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("last_login_time", new Date())
                .eq("id", userId);
        this.update(updateWrapper);
    }
}
